﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FAST
{
    public partial class SessionUI : Form
    {
        private MainUI mainUI;

        public SessionUI(MainUI main)
        {
            InitializeComponent();
            mainUI = main;
            this.sessionListView.MouseDoubleClick += new MouseEventHandler(sessionListView_MouseDoubleClick);
            
            this.MouseHover += new EventHandler(this.form_MouseHover);
            hoverPanel.MouseHover += new EventHandler(this.hoverPanel_MouseHover);
            sessionListView.MouseHover += new EventHandler(this.form_MouseHover);
        }
        
        public void form_MouseHover(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
        }

        public void hoverPanel_MouseHover(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.Sizable;
        }

        public void SessionUI_Load(object sender, EventArgs e)
        {
            this.Show();
        }

        public void addListViewItem(ListViewItem item)
        {
            sessionListView.Items.Add(item);
        }

        private void sessionListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DialogResult dMsg = MessageBox.Show("Are you sure want to load this session?", "FAST", MessageBoxButtons.YesNo);//No selection yet -> default is Yes
            if (dMsg == DialogResult.Yes)
            {
                mainUI.loadSessionByIndex(sessionListView.SelectedItems[0].Index);
                this.Dispose();
                mainUI.Enabled = true;
                mainUI.Focus();
            }
        }

        private void newSession_Click(object sender, EventArgs e)
        {
            mainUI.clickNewSessionToolStripMenuItem();
            this.Dispose();
            mainUI.Enabled = true;
            mainUI.Focus();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            int count = sessionListView.Items.Count;
            if (count != 0 && e.CloseReason == CloseReason.UserClosing)
            {   this.Hide();
                MessageBox.Show("Please select a session.");
                this.Show();
                e.Cancel = true;
            }
            else if (count == 0 && e.CloseReason == CloseReason.UserClosing) 
            {
                mainUI.Enabled = true;
                mainUI.Focus();
                base.OnFormClosing(e);           
            }
        }

        private void ok_Click(object sender, EventArgs e)
        {

            int count = sessionListView.Items.Count;
            try
            {
                if (count == 0)
                {
                    this.Dispose();
                    mainUI.Enabled = true;
                    mainUI.Focus();
                }
                else
                {
                    for (int i = 0; i < sessionListView.Items.Count; i++)
                    {
                        if (sessionListView.Items[i].Selected == true)
                        {
                            mainUI.loadSessionByIndex(sessionListView.Items[i].Index);
                            this.Dispose();
                            mainUI.Enabled = true;
                            mainUI.Focus();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void sessionListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            ok.Enabled = true;
          
        }

    }
}
